package com.xy.Servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.dao.impl.PageResults;
import com.xy.model.BJi;
import com.xy.model.Category;
import com.xy.model.Level;
import com.xy.model.Major;
import com.xy.service.ClassService;
import com.xy.service.impl.CategoryServiceImpl;
import com.xy.service.impl.ClassServiceImpl;
import com.xy.service.impl.LevelServiceImpl;
import com.xy.service.impl.MajorServiceImpl;

/**
 * Servlet implementation class ClassServlet
 */
@WebServlet("/classServlet")
public class ClassServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  /**
   * @see HttpServlet#HttpServlet()
   */
  public ClassServlet() {
    super();
    // TODO Auto-generated constructor stub
  }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(request, response);
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    ClassService service = new ClassServiceImpl();

    if ("goadd".equals(request.getParameter("status"))) {
      List<Object> clist = new CategoryServiceImpl().list(Category.class);
      List<Object> mlist = new MajorServiceImpl().list(Major.class);
      List<Object> llist = new LevelServiceImpl().list(Level.class);
      request.setAttribute("clist", clist);
      request.setAttribute("mlist", mlist);
      request.setAttribute("llist", llist);
      request.getRequestDispatcher("/pages/student/student003/add_class.jsp").forward(request,
          response);

    } else if ("goquery".equals(request.getParameter("status"))) {
      List<Object> clist = new CategoryServiceImpl().list(Category.class);
      List<Object> mlist = new MajorServiceImpl().list(Major.class);
      List<Object> llist = new LevelServiceImpl().list(Level.class);
      List<Object> blist = service.list(BJi.class);
      request.setAttribute("clist", clist);
      request.setAttribute("mlist", mlist);
      request.setAttribute("llist", llist);
      request.setAttribute("blist", blist);
      request.getRequestDispatcher("/pages/student/student003/query_class.jsp").forward(request,
          response);

    } else if ("goup".equals(request.getParameter("status"))) {
      Integer id = Integer.valueOf(request.getParameter("id"));
      BJi bj = (BJi) service.get(id);
      List<Object> clist = new CategoryServiceImpl().list(Category.class);
      List<Object> mlist = new MajorServiceImpl().list(Major.class);
      request.setAttribute("bj", bj);
      request.setAttribute("clist", clist);
      request.setAttribute("mlist", mlist);
      request.getRequestDispatcher("/pages/student/student003/modify_class.jsp").forward(request,
          response);
      
    }else if ("add".equals(request.getParameter("status"))) {
      BJi bj = new BJi();
      bj.setName(request.getParameter("name"));
      bj.setRemark(request.getParameter("remark"));
      bj.setGrade(request.getParameter("grade"));
      bj.setCategory((Category) new CategoryServiceImpl()
          .get(Integer.valueOf(request.getParameter("category"))));
      bj.setLevel(
          (Level) new LevelServiceImpl().get(Integer.valueOf(request.getParameter("level"))));
      bj.setMajor(
          (Major) new MajorServiceImpl().get(Integer.valueOf(request.getParameter("major"))));
      bj.setCreateDate(new Date());
      if (service.add(bj)) {
        request.getRequestDispatcher("/classServlet?status=list&currentPage=0&radio=n").forward(request,
            response);
      } else {
        System.out.println("error");
        request.getRequestDispatcher("/pages/student/student003/add_class.jsp").forward(request,
            response);
      }

    } else if ("list".equals(request.getParameter("status"))) {
      int currentPage = 0;
      if (request.getParameter("goalpage") == null) {
        currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
      } else
        currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;
      String list = null;
      String name = null;
      String category = null;
      String level = null;
      String major = null;
      String grade = null;
      String bj = null;

      PageResults pageResults = null;
      
      if(request.getParameter("radio")!=null) {
        request.getSession().setAttribute("radio", request.getParameter("radio"));
      }
      
      if ("n".equals(request.getSession().getAttribute("radio"))) {
        if (request.getParameter("list") == null) {
          name = request.getParameter("name") == null ? "%" : request.getParameter("name");
          list = name;
        } else {
          list = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
          name = list;
        }
        pageResults = service.query(name, currentPage);
        request.setAttribute("pageResults", pageResults);
        request.setAttribute("status", "list");
        request.getSession().setAttribute("radio", "n");
        request.setAttribute("list", list);

      } else {
        if (request.getParameter("list") == null) {
          category =
              request.getParameter("category") == null ? "%" : request.getParameter("category");
          level = request.getParameter("level") == null ? "%" : request.getParameter("level");
          major = request.getParameter("major") == null ? "%" : request.getParameter("major");
          grade = request.getParameter("grade") == null ? "%" : request.getParameter("grade");
          bj = request.getParameter("bj") == null ? "%" : request.getParameter("bj");
          list = category + "," + level + "," + major + "," + grade + "," + bj;
        } else {
          String str = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
          String[] strs = str.split(",");
          for (int i = 0; i < strs.length; i++) {
            if (i == 0)
              category = i == 0 ? strs[i] : "%";
            if (i == 1)
              level = i == 1 ? strs[1] : "%";
            if (i == 2)
              major = i == 2 ? strs[2] : "%";
            if (i == 3)
              grade = i == 3 ? strs[3] : "%";
            if (i == 4)
              bj = i == 4 ? strs[4] : "%";
          }
          list = category + "," + level + "," + major + "," + grade + "," + bj;
        }
        pageResults = service.queryByC(category, level, major, grade, bj, currentPage);
        request.setAttribute("pageResults", pageResults);
        request.setAttribute("status", "list");
        request.getSession().setAttribute("radio", "c");
        request.setAttribute("list", list);
      }
      
      request.getRequestDispatcher("/pages/student/student003/query_result.jsp").forward(request,
          response);

    } else if ("del".equals(request.getParameter("status"))) {
      Integer id = Integer.valueOf(request.getParameter("id"));
      BJi bj = (BJi) service.get(id);
      if(service.delete(bj)) {
        request.getRequestDispatcher("/classServlet?status=list&currentPage=0&radio=n").forward(request,
            response);
      }else {
        System.out.println("error");
        request.getRequestDispatcher("/classServlet?status=list&currentPage=0&radio=n").forward(request,
            response);
      }
    } else if ("up".equals(request.getParameter("status"))) {
      Integer id = Integer.valueOf(request.getParameter("id"));
      BJi bj = (BJi) service.get(id);
      bj.setName(request.getParameter("name"));
      bj.setRemark(request.getParameter("remark"));
      bj.setCategory((Category) new CategoryServiceImpl()
          .get(Integer.valueOf(request.getParameter("category"))));
      bj.setMajor(
          (Major) new MajorServiceImpl().get(Integer.valueOf(request.getParameter("major"))));
      if (service.update(bj)) {
        request.getRequestDispatcher("/classServlet?status=list&currentPage=0&radio=n&name="+request.getParameter("name")).forward(request,
            response);
      } else {
        System.out.println("error");
        request.getRequestDispatcher("/classServlet?status=list&currentPage=0&radio=n&name=%").forward(request,
            response);
      }
    } else if ("detail".equals(request.getParameter("status"))) {

    }
  }

}
